package com.itcast.donkey.popwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.itcast.donkey.R;
import com.itcast.donkey.adapters.PopLangueListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/9/9.
 */
public class PopEndLangueList extends PopupWindow {

    private ListView listPop;
    private View view;
    private String langue[];
    private List<String> stringList = new ArrayList<>();
    private PopLangueListAdapter adapter;

    public PopEndLangueList(Context context, ListView.OnItemClickListener onItemClickListener, String[] langue) {
        super(context);
        this.langue = langue;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.pop_langue_list, null);
        listPop = (ListView) view.findViewById(R.id.list_pop);

        for (int i = 0; i < langue.length; i++) {
            stringList.add(i, langue[i]);
        }

        adapter = new PopLangueListAdapter(stringList, context);
        listPop.setAdapter(adapter);
        listPop.setOnItemClickListener(onItemClickListener);

        //显示弹出框
        this.setContentView(view);
        this.setWidth(240);
        this.setHeight(500);
        //设置弹窗可点击
        this.setFocusable(true);
        //设置弹出窗体的动画效果
        this.setAnimationStyle(R.style.AppTheme);
        ColorDrawable cd = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(cd);

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = view.findViewById(R.id.lay_pop).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}
